package gui;

import javax.swing.AbstractListModel;
import javax.swing.ComboBoxModel;
import javax.swing.DefaultComboBoxModel;
import javax.swing.DefaultListModel;
import javax.swing.JPanel;
import java.awt.BorderLayout;
import javax.swing.JComboBox;

import java.awt.Dimension;
import java.awt.GridBagLayout;
import java.awt.GridBagConstraints;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.util.ArrayList;
import java.util.Vector;

import javax.swing.JList;
import javax.swing.event.ListDataListener;

import observer.Broker;
import observer.Observer;
import utility.Auto;

public class StatistichePanel extends JPanel implements ActionListener, Observer, ItemListener{

	private JComboBox comboBoxAutos;
	private JList listAutosProprieties;

	private DefaultComboBoxModel comboModel;
	private DefaultListModel listModel;

	private Broker broker;

	/**
	 * Create the panel.
	 */
	 public StatistichePanel(Broker broker) {


		 this.broker = broker;


		 broker.addObserver(this);

		 this.setPreferredSize(new Dimension(250, 600));

		 GridBagLayout gridBagLayout = new GridBagLayout();
		 gridBagLayout.columnWidths = new int[]{0, 0, 0, 158, 158, 0, 0, 0, 0};
		 gridBagLayout.rowHeights = new int[]{0, 0, 33, 50, 0};
		 gridBagLayout.columnWeights = new double[]{0.0, 0.0, 0.0, 1.0, 1.0, 0.0, 0.0, 0.0, Double.MIN_VALUE};
		 gridBagLayout.rowWeights = new double[]{0.0, 0.0, 0.0, 1.0, Double.MIN_VALUE};
		 setLayout(gridBagLayout);

		 comboBoxAutos = new JComboBox();
		 //                comboModel = new DefaultComboBoxModel(broker.getAutoBroker());
		 //                comboBoxAutos.setModel(comboModel);
		 comboBoxAutos.addItemListener(this);

		 GridBagConstraints gbc_comboBoxAutos = new GridBagConstraints();
		 gbc_comboBoxAutos.gridwidth = 2;
		 gbc_comboBoxAutos.fill = GridBagConstraints.HORIZONTAL;
		 gbc_comboBoxAutos.insets = new Insets(0, 0, 5, 5);
		 gbc_comboBoxAutos.gridx = 3;
		 gbc_comboBoxAutos.gridy = 1;
		 add(comboBoxAutos, gbc_comboBoxAutos);

		 listAutosProprieties = new JList();
		 listModel = new DefaultListModel();
		 listAutosProprieties.setModel(listModel);

		 GridBagConstraints gbc_listAutosProprieties = new GridBagConstraints();
		 gbc_listAutosProprieties.gridwidth = 2;
		 gbc_listAutosProprieties.insets = new Insets(0, 0, 0, 5);
		 gbc_listAutosProprieties.fill = GridBagConstraints.BOTH;
		 gbc_listAutosProprieties.gridx = 3;
		 gbc_listAutosProprieties.gridy = 3;
		 add(listAutosProprieties, gbc_listAutosProprieties);

	 }


	 @Override
	 public void actionPerformed(ActionEvent e) {
		 // TODO Auto-generated method stub

	 }


	 private class ComboModel extends DefaultComboBoxModel {

		 private String madeChoice;

		 private Auto autoSelected;


		 //                public ComboModel() {
			 //                        //this.autos = autos;
		 ////                      if(broker.getAutoBroker().isEmpty())
		 //                                this.madeChoice = "";
		 ////                      else
		 ////                              this.madeChoice = ""+broker.getAutoBroker().get(0).getId();
		 //                }
		 //
		 //                @Override
		 //                public Object getElementAt(int index) {
		 //                        
		 //                        autoSelected = broker.getAutoBroker().get(index);
		 //                        madeChoice = ""+autoSelected.getId();
		 //                        return madeChoice;
		 //                }
		 //
		 //                @Override
		 //                public int getSize() {
		 //                        return broker.getAutoBroker().size();
		 //                }
		 //
		 //                @Override
		 //                public Object getSelectedItem() {
		 //                        
		 //                        return madeChoice;
		 //                }
		 //
		 //                @Override
		 //                public void setSelectedItem(Object arg0) {
		 //                        madeChoice=arg0.toString();
		 ////                        System.out.println("++++++++++++"+madeChoice);
		 ////                        
		 //////                      Auto auto = broker.getNavigatore().getAuto((Integer) arg0);
		 ////                        
		 ////                        listModel.removeAllElements();
		 ////                        
		 ////                        listModel.addElement(autoSelected.getId());
		 ////                        listModel.addElement(autoSelected.getStart().getThird());
		 ////                        listModel.addElement(autoSelected.getEnd().getThird());
		 ////                        listModel.addElement(autoSelected.getWay().get(0).getStreetName());
		 ////                        listModel.addElement(autoSelected.getSpeed());
		 ////                        
		 ////                        broker.notifyObservers("refreshStatistics");
		 //
		 //                }
	 }

	 //      private class ListModel extends DefaultListModel{
	 //
	 //              
	 //              
	 //      }

	 @Override
	 public void update(Object object) {

		 if(object.equals("refreshStatistics"))
			 listAutosProprieties.updateUI();

		 if(object.equals("pippo")) {

			 if(comboBoxAutos.getItemCount() != 0) {
				 comboBoxAutos.removeAllItems();
				 System.out.println("wwwwwwwwwwwwww  "+broker.getAutoBroker().size());
				 for (Auto car : broker.getAutoBroker()) {
					 comboBoxAutos.addItem(car);
				 }

				 comboBoxAutos.updateUI();
			 }
			 else {
				 comboModel = new DefaultComboBoxModel(broker.getAutoBroker());
				 comboBoxAutos.setModel(comboModel);
			 }
		 }
	 }


	 @Override
	 public void itemStateChanged(ItemEvent e) {

		 if (e.getSource() == comboBoxAutos) {
			 int i = comboBoxAutos.getSelectedIndex();
			 System.out.println("iiiiiiiiiiii   "+i);
			 if(comboBoxAutos.getSelectedIndex() > 0) {
				 Auto autoSelected = broker.getAutoBroker().get(comboBoxAutos.getSelectedIndex());

				 listModel.removeAllElements();

				 listModel.addElement(autoSelected.getId());
				 listModel.addElement(autoSelected.getStart().getThird());
				 listModel.addElement(autoSelected.getEnd().getThird());
				 listModel.addElement(autoSelected.getWay().get(0).getStreetName());
				 listModel.addElement(autoSelected.getSpeed());

				 broker.notifyObservers("refreshStatistics");
			 }
		 }
	 }

}
